﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Drawing.Imaging;

namespace Manager
{
    [Serializable]
    public class WriteFile
    {

        protected enum _DataKind { _cbSprite = 1, _cbSound = 2, _cbAnimation = 3 };

        #region Memory Managers

        private static Stream pStream;
        private static MemoryStream mStream = new MemoryStream();
        private static GZipStream gCompress;
       // private static StreamReader sReader;
        private static IFormatter Format = new BinaryFormatter();
        
        #endregion

        #region Header Variables
        public static string pFilePath = "";
        public static string pFileName = "noname";
        private static UInt32 numFiles = 900;
        private static string EncVersion = "1.0";
        private static int fileKind = 1;
        private static byte[] ChunkBytes; 
        private static string[] FileList = new string[numFiles];

       


        #endregion

        #region Write in Binary Files

        private static void WriteHeader(List<Bitmap> Images, _DataKind  Kind)
        {
            switch (Kind)
            {
                case _DataKind._cbAnimation:
                    fileKind = Convert.ToInt32(_DataKind._cbAnimation);
                    break;
                case _DataKind._cbSound:
                    fileKind = Convert.ToInt32(_DataKind._cbSound);
                    break;
                case _DataKind._cbSprite:
                    fileKind = Convert.ToInt32(_DataKind._cbSprite);
                    break;
                default:
                    fileKind = Convert.ToInt32(_DataKind._cbSprite);
                    break;



            }


            Format.Serialize(pStream, EncVersion);
            Format.Serialize(pStream, fileKind);
            Format.Serialize(pStream, numFiles);
            Format.Serialize(pStream, fileKind);
            Format.Serialize(pStream, DateTime.Now.Month);

           
            
           
            


        }


        private static void WriteFiles(List<Bitmap> ListBitmaps)
        {

            try
            {
                for (int i = 0; i <= ListBitmaps.Count; i++)
                {
                    if (ListBitmaps[i] != null)
                    {
                        ListBitmaps[i].Save(mStream, ImageFormat.Jpeg);
                        numFiles++;
                    }
                }



            }
            catch (Exception ex)
            {
                Manager.MessageError.ShowError(ex.Message, "Erro");

            }
            finally
            {
                ChunkBytes = mStream.GetBuffer();
                Format.Serialize(pStream, ChunkBytes);

            }



        }
        #endregion

        public static void doWriteFile(List<Bitmap> List)
        {

            try
            {

                pStream = File.Open(pFilePath + pFileName, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
                ChunkBytes = new byte[mStream.Length];
                gCompress = new GZipStream(pStream, CompressionMode.Compress);
                WriteFiles(List);
                pStream.Seek(0, SeekOrigin.Begin);
                WriteHeader(List, _DataKind._cbSprite);
                pStream.Seek(0, SeekOrigin.End);
                pStream.Flush();
                pStream.Close();
            }
            catch (Exception errorEx)
            {

                Manager.MessageError.ShowError(errorEx.Message, "Error");
                

            }
        


        }

        public static void LoadFile(string path)
        {
            Stream StreamFile = File.Open(path, FileMode.Open, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
           
            
         
  



        }

    

    }
}
